<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=995943
-->
<window title="Mozilla Bug 995943"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=995943"
     target="_blank">Mozilla Bug 995943</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  const Cu = Components.utils;
  const Cc = Components.classes;
  const Ci = Components.interfaces;
  Cu.import("resource://gre/modules/Services.jsm");
  function debug(msg) { info(msg); }

  /** Test for CAPS file:// URI prefs. **/
  SimpleTest.waitForExplicitFinish();
  SimpleTest.requestCompleteLog();
  if (navigator.userAgent.indexOf("Mac OS X 10.10") != -1)
    SimpleTest.expectAssertions(5, 11); // See bug 1067022, 1307988
  else if (Services.appinfo.OS == "WINNT")
    SimpleTest.expectAssertions(0, 1); // See bug 1067022
  else
    SimpleTest.expectAssertions(0, 2); // See bug 1305241

  var rootdir = Services.appinfo.OS == "WINNT" ? "file:///C:" : "file:///";

  function checkLoadFileURI(domain, shouldLoad) {
    debug("Invoking checkLoadFileURI with domain: " + domain + ", shouldLoad: " + shouldLoad);
    return new Promise(function(resolve, reject) {
      $('ifr').addEventListener('load', function l1() {
        debug("Invoked l1 for " + domain);
        $('ifr').removeEventListener('load', l1);
        function l2() {
          debug("Invoked l2 for " + domain);
          $('ifr').removeEventListener('load', l2);
          ok(shouldLoad, "Successfully loaded file:// URI for domain: " + domain);
          resolve();
        }
        $('ifr').addEventListener('load', l2);
        try {
          window[0].wrappedJSObject.location = rootdir;
          debug("Successfully navigated for " + domain);
        } catch (e) {
          ok(!shouldLoad && /denied|insecure/.test(e),
             "Prevented loading of file:// URI for domain: " + domain + " - " + e);
          $('ifr').removeEventListener('load', l2);
          resolve();
        }
      });
      let targetURI = domain + '/tests/js/xpconnect/tests/mochitest/file_empty.html';
      debug("Navigating iframe to " + targetURI);
      $('ifr').contentWindow.location = targetURI;
    });
  }

  function pushPrefs(prefs) {
    return new Promise(function(resolve) { SpecialPowers.pushPrefEnv({ set: prefs }, resolve); });
  }

  function popPrefs() {
    return new Promise(function(resolve) { SpecialPowers.popPrefEnv(resolve); });
  }

  var gGoCount = 0;
  function go() {
    debug("Invoking go for window with id: " + window.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID);
    is(++gGoCount, 1, "Should only call go once!");
    checkLoadFileURI('http://example.com', false).then(
      pushPrefs.bind(null, [['capability.policy.policynames', ' somepolicy '],
                            ['capability.policy.somepolicy.checkloaduri.enabled', 'AlLAcCeSs'],
                            ['capability.policy.somepolicy.sites', 'http://example.com']]))
    .then(checkLoadFileURI.bind(null, 'http://example.com', true))
    .then(popPrefs)
    .then(checkLoadFileURI.bind(null, 'http://example.com', false))
    .then(
      pushPrefs.bind(null, [['capability.policy.policynames', ',somepolicy, someotherpolicy, '],
                            ['capability.policy.somepolicy.checkloaduri.enabled', 'allaccess'],
                            ['capability.policy.someotherpolicy.checkloaduri.enabled', 'nope'],
                            ['capability.policy.somepolicy.sites', ' http://example.org   test1.example.com https://test2.example.com '],
                            ['capability.policy.someotherpolicy.sites', 'http://example.net ']]))
    .then(checkLoadFileURI.bind(null, 'http://example.org', true))
    .then(checkLoadFileURI.bind(null, 'http://test2.example.com', false))
    .then(checkLoadFileURI.bind(null, 'https://test2.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://sub1.test2.example.com', false))
    .then(checkLoadFileURI.bind(null, 'https://sub1.test2.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://example.net', false))
    .then(checkLoadFileURI.bind(null, 'http://test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'https://test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'http://sub1.test1.example.com', true))
    .then(checkLoadFileURI.bind(null, 'https://sub1.test1.example.com', true))
    .then(pushPrefs.bind(null, [['capability.policy.someotherpolicy.checkloaduri.enabled', 'allAccess']]))
    .then(checkLoadFileURI.bind(null, 'http://example.net', true))
    .then(popPrefs)
    .then(popPrefs)
    .then(checkLoadFileURI.bind(null, 'http://example.net', false))
    .then(SimpleTest.finish.bind(SimpleTest));

  }
  addLoadEvent(go);

  ]]>
  </script>
  <iframe id="ifr" type="content" />
</window>
